﻿using System;
using System.Web.UI;
using BLL;
using Utility;
using Entity;

namespace StudentAppointmentPlatform
{
    public partial class AddService : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string loginType = Session["LoginType"] as string;
                if (loginType == null || loginType != "Director")
                {
                    Alert.AlertOnPage(Page, "此页面只允许[负责人]查看，您可能没有权限，即将跳转到默认页面。");
                    Redirect.Js_Redirect(Page, "/Default.aspx");
                }

                tbDirectorName.Text = Session["UserName"].ToString();
            }
        }

        protected void btnCreateService_OnClick(object sender, EventArgs e)
        {
            AppointmentService service = new AppointmentService();

            string userId = Session["UserId"] as string;
            service.DirectorId = UserBO.GetIdByUserId(userId);
            service.ServiceName = tbServiceName.Text;
            service.TimeBuckets = tbTimeBuckets.Text;
            service.Introduction = tbIntroduction.Text;

            float perMultiTime;
            bool isCorrect = float.TryParse(tbPerMultiTime.Text, out perMultiTime);
            if (isCorrect)
            {
                service.PerMultiTime = perMultiTime;
            }
            else
            {
                Alert.AlertOnPage(Page, "您填写的时间分段数值似乎不正确，操作中断！");
                return;
            }

            int multiNum;
            if (int.TryParse(tbMultiNum.Text, out multiNum))
            {
                service.MultiNum = multiNum;
            }
            else
            {
                Alert.AlertOnPage(Page, "您填写的每个时间分段允许的人数似乎不正确，操作中断！");
                return;
            }           

            if (ServiceBO.InsertService(service) > 0)
            {
                Alert.AlertOnPage(Page, "创建服务成功！");
            }
            else
            {
                Alert.AlertOnPage(Page, "创建服务失败！");
            }

        }
    }
}